﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ShiBaiPanel : MonoBehaviour
{
    public static ShiBaiPanel instance;
    public GameObject can, gamepanel, Voiceobj;
    public Button QuXiaoButton, KanGuangGaoButton;
    public GameObject player;
    public bool ShanChuFirst;
    public bool CloneSecond;
    public bool FinishWatch;
    void Start()
    {
        instance = this;
        CloneVoice();
        GoldAndLv.instance.audio.Stop();
        can = GameObject.Find("Canvas");
        player = GameObject.Find("Player");
        gamepanel = GameObject.FindGameObjectWithTag("GamePanel");
        QuXiaoButton.onClick.AddListener(OnClickQuXiaoButton);
        KanGuangGaoButton.onClick.AddListener(GuangGaoButton);
        if (GamePanel.instance.FuHuo == 1)
        {
            KanGuangGaoButton.interactable = false;
        }
    }
    /// <summary>
    /// 克隆短音效
    /// </summary>
    public void CloneVoice()
    {
        Voiceobj = Instantiate(Resources.Load<GameObject>("CloneVoice"));
        Voiceobj.transform.SetParent(gameObject.transform);
    }

    public void OnClickQuXiaoButton()
    {
        ShortVoice.instance.PlayVoice("AnNiu");
        if (!ShanChuFirst)
        {
            DestroyImmediate(gameObject);
            DestroyImmediate(gamepanel);
            DestroyImmediate(Voiceobj);
            CloneSecond = true;
        }
        if (CloneSecond)
        {
            GameObject obj = Instantiate(Resources.Load<GameObject>("UIPanel/MainPanel"));
            obj.transform.SetParent(can.transform);
            obj.transform.localPosition = Vector3.zero;
            obj.transform.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0);
            obj.transform.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0);
            obj.transform.GetComponent<RectTransform>().anchorMin = Vector2.zero;
            obj.transform.GetComponent<RectTransform>().anchorMax = Vector2.one;
            player.GetComponent<BoxCollider2D>().enabled = true;
            GoldAndLv.instance.PlayVoice();
        }

    }

    private void Update()
    {
        if (FinishWatch)
        {
            RealFuHuo();
        }
    }
    /// <summary>
    /// /点击广告按钮
    /// </summary>
    public void GuangGaoButton()
    {
        ShortVoice.instance.PlayVoice("AnNiu"); 
        GoogleMobileAdsDemoScript.instance.ShowRewardedAd();
        
       
    }
    /// <summary>
    /// 复活后
    /// </summary>
    public void RealFuHuo()
    {
        DestroyImmediate(gameObject);
        GamePanel.instance.IsDeath = false;
        player.GetComponent<BoxCollider2D>().enabled = false;
        GamePanel.instance.FuHuo++;
        GamePanel.instance.YiFuHuo = true;
        GamePanel.instance.ZheZhao.SetActive(false);
        GoldAndLv.instance.PlayVoice();
        DestroyImmediate(Voiceobj);
        FinishWatch = false;
    }
   
   
}
